package mumethod;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import mudbconnection.DBPhysicals;
import muobject.Physical;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("AccessPhysicalsService")
public class AccessPhysicalsService implements AccessPhysicals {

	@Autowired
	private DBPhysicals dbPhysicals;
	private List<Physical> physicals;
	private List<String> testDates;
	private Physical physical;
	private ValidFunction validFunction;
	
	@SuppressWarnings("rawtypes")
	private List rows;
	private DateControl dateControl;
	
	public List<Physical> getAllPhysical(){
		physicals =  dbPhysicals.getAllPhysicals();
		return physicals;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<String> getAllTestDate(String sno, String year, String grade, String sclass) {
		dateControl = new LibFunction();
		rows = dbPhysicals.getAllTestDate(sno, year, grade, sclass);
		testDates = new ArrayList();
		Iterator it = rows.iterator();
		while (it.hasNext()) {
			Map testDate = (Map) it.next();
			if (testDate.get("TestDate") != null)
				testDates.add(dateControl.convertDate(String.valueOf(testDate.get("TestDate"))));
		}
		return testDates;
	}
	
	public Physical getSpicalStudentYearTestdatePhysicals(String stno, String year, String testDate){
		validFunction = new ValidFunction();
		physical =  dbPhysicals.getSpicalStudentYearTestdatePhysicals(stno, year, testDate);
		return validFunction.PhysicalNullReturn(physical);
	}
	
	public boolean existPhysical(String stno, String year, String testDate) {
		return dbPhysicals.getSpicalStudentYearTestdatePhysicals(stno, year, testDate)!= null;
	}
	
	public boolean setPhysicalFromTable(Table table, String yeararg){
		String stno, years, block; 
		Date testDate;
		double height, weight, grip, jump, situp, vcapacity, run69, run15;
		dateControl = new LibFunction();
		try {
			for (TableItem item : table.getItems()) {
				block = String .valueOf(item.getChecked());
				testDate = Date.valueOf(item.getText(1));
				stno = item.getText(2);
				years = yeararg;
				height = validFunction.doubleReturn(item.getText(8));
				weight = validFunction.doubleReturn(item.getText(9));
				grip = validFunction.doubleReturn(item.getText(10));
				jump = validFunction.doubleReturn(item.getText(11));
				situp = validFunction.doubleReturn(item.getText(12));
				vcapacity = validFunction.doubleReturn(item.getText(13));
				run69 = validFunction.doubleReturn(item.getText(14));
				run15 = validFunction.doubleReturn(item.getText(15));
				physical = new Physical(0, stno, years, testDate, height, weight, grip, jump, situp, vcapacity, run69, run15, block, null, null);
				if(!(height == 0 & weight == 0 & grip == 0 & jump == 0 & situp == 0 & vcapacity == 0 & run69 == 0 & run15 == 0)){
					if(!existPhysical(stno, years, dateControl.convertDate(String.valueOf(testDate)))){
						if(!dbPhysicals.setPhysical(physical)){
							return false;
						}
					}else{
						if(!dbPhysicals.updatePhysical(physical)){
							return false;
						}
					}
				}else{
					dbPhysicals.deletePhysical(stno, years, dateControl.convertDate(String.valueOf(testDate)));
				}
			}
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	public boolean deletePhysicalFromTable(Table table, String yeararg){
		String stno, years, block; 
		Date testDate;
		double height, weight, grip, jump, situp, vcapacity, run69, run15;
		dateControl = new LibFunction();
		try {
			for (TableItem item : table.getItems()) {
				block = String .valueOf(item.getChecked());
				testDate = Date.valueOf(item.getText(1));
				stno = item.getText(2);
				years = yeararg;
				height = validFunction.doubleReturn(item.getText(8));
				weight = validFunction.doubleReturn(item.getText(9));
				grip = validFunction.doubleReturn(item.getText(10));
				jump = validFunction.doubleReturn(item.getText(11));
				situp = validFunction.doubleReturn(item.getText(12));
				vcapacity = validFunction.doubleReturn(item.getText(13));
				run69 = validFunction.doubleReturn(item.getText(14));
				run15 = validFunction.doubleReturn(item.getText(15));
				physical = new Physical(0, stno, years, testDate, height, weight, grip, jump, situp, vcapacity, run69, run15, block, null, null);
				if(!(height == 0 & weight == 0 & grip == 0 & jump == 0 & situp == 0 & vcapacity == 0 & run69 == 0 & run15 == 0)){
					if(existPhysical(stno, years, dateControl.convertDate(String.valueOf(testDate)))){
						dbPhysicals.deletePhysical(stno, years, dateControl.convertDate(String.valueOf(testDate)));
					}
				}
			}
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	public boolean deletePhysicalByPar(String stno, String year, String testDate){
		try{
			dbPhysicals.deletePhysical(stno, year, dateControl.convertDate(String.valueOf(testDate)));
			return true;
		}catch(Exception e){
			return false;
		}
	}
}
